<?php
/**
 +------------------------------------------------------------------------------
 * array2xml
 +------------------------------------------------------------------------------
 * @Version 1.2
 * @Author  WangXian
 * @E-mail   admin@loopx.cn
 * @package ephp
 * @FileName Xml.class.php
 * @Creation date 2010-10-15
 * @Modified date 2010-10-15
 +------------------------------------------------------------------------------
 */
class Xml
{
	// xml编码
	public function xml_encode($data,$encoding='utf-8',$root="root") {
	    $xml = '<?xml version="1.0" encoding="'.$encoding.'"?>';
	    $xml.= '<'.$root.'>';
	    $xml.= $this->data_to_xml($data);
	    $xml.= '</'.$root.'>';
	    return $xml;
	}
	
	private function data_to_xml($data) {
	    if(is_object($data))
	        $data = get_object_vars($data);
	    $xml = '';
	    foreach($data as $key=>$val)
	    {
	        is_numeric($key) && $key="item id=\"$key\"";
	        $xml.="<$key>";
	        $xml.=(is_array($val)||is_object($val))?$this->data_to_xml($val):$val;
	        list($key,)=explode(' ',$key);
	        $xml.="</$key>";
	    }
	    return $xml;
	}
}